package luj.tool.jnproxyan.framework.lujnet.receive;

import io.netty.buffer.ByteBuf;
import luj.net.api.server.FrameDataReceiver;
import luj.tool.jnproxyan.framework.lujnet.ConnState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
final class Recv32MethodHeader implements FrameDataReceiver {

  @Override
  public Result receive(Context ctx) {
    ByteBuf frameBuf = ctx.getLastFrame();
    ConnState state = ctx.getConnectionState();

    int version = frameBuf.readByte();
    int nMethods = frameBuf.readByte();
    state.setVer(version);
    state.setnMethods(nMethods);

//    LOG.debug("{} -> version: {}", state.getC2pConnection(), version);
//    LOG.debug("nMethods: {}", nMethods);

    return ctx.then()
        .waitBytes(nMethods)
        .nextReceiver(_receiveMethod);
  }

  private static final Logger LOG = LoggerFactory.getLogger(Recv32MethodHeader.class);

  @Autowired
  Recv33Methods _receiveMethod;
}
